home *** CD-ROM | disk | FTP | other *** search
/ Aminet 8 / Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso / Aminet / util / cli / iuw_clitools.lha / src / setrc.c < prev    next >
C/C++ Source or Header  |  1995-09-05  |  3KB  |  107 lines

  1. /* setrc - set return code, similar to Un*x' /bin/true and /bin/false
  2.  *
  3.  * Copyright (C) 1994 by Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose and without fee is hereby granted, provided
  7.  * that the above copyright notice appear in all copies and that both that
  8.  * copyright notice and this permission notice appear in supporting
  9.  * documentation.  This software is provided "as is" without express or
  10.  * implied warranty.
  11.  *
  12.  * V1.0: 17/Apr/94 first version
  13.  * V1.1: 11/Aug/94 added program name check
  14.  * V1.2: 09/Feb/95 added result2
  15.  */
  16. #define THIS_PROGRAM    "setrc"
  17. #define THIS_VERSION    "1.2"
  18.  
  19. #include <exec/types.h>
  20. #include <exec/libraries.h>
  21. #include <dos/dos.h>
  22. #include <dos/rdargs.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25.  
  26. #include <proto/exec.h>
  27. #include <proto/dos.h>
  28.  
  29. static char amiga_version[] = "\0$VER: " THIS_PROGRAM " " THIS_VERSION " (" __COMMODORE_DATE__ ")";
  30.  
  31. char Template[] = "OK/S,WARN/S,ERROR/S,FAIL/S,RC=RESULT/N,RC2=RESULT2/N";
  32. __aligned struct {
  33.     LONG    ok;
  34.     LONG    warn;
  35.     LONG    error;
  36.     LONG    fail;
  37.     LONG   *rc;
  38.     LONG   *rc2;
  39. } Args;
  40.  
  41. #define NAMELEN     256
  42.  
  43. const struct tbl_t {
  44.     const char *name;
  45.     const LONG  rc;
  46. } table[] = {
  47.     { "true",   1 },    /* IF `true` */
  48.     { "false",  0 },
  49.     { "fail",  20 },
  50.     { "error", 10 },
  51.     { "warn",   5 },
  52.     { "ok",     0 },
  53.     { NULL,     0 }
  54. };
  55.  
  56.  
  57. void
  58. _main()
  59. {
  60.     struct RDArgs *rdargs;
  61.     LONG rc = RETURN_OK;
  62.  
  63.     if( SysBase->lib_Version < 37 ) {
  64.         #define MESSAGE "requires AmigaOS 2.04 or higher\n"
  65.         Write(Output(), MESSAGE, sizeof(MESSAGE)-1);
  66.         _exit(RETURN_FAIL);
  67.         #undef MESSAGE
  68.     }
  69.  
  70.     if( rdargs = ReadArgs(Template, (LONG *)&Args, NULL) ) {
  71.         if( Args.rc )
  72.             rc = *(Args.rc);
  73.         else
  74.         if( Args.fail )
  75.             rc = RETURN_FAIL;
  76.         else
  77.         if( Args.error )
  78.             rc = RETURN_ERROR;
  79.         else
  80.         if( Args.warn )
  81.             rc = RETURN_WARN;
  82.         else
  83.         if( Args.ok )
  84.             rc = RETURN_OK;
  85.         else {
  86.             char progname[NAMELEN];
  87.             if( GetProgramName(progname, NAMELEN) ) {
  88.                 struct tbl_t *p;
  89.                 for( p = table; p->name; p++ ) {
  90.                     if( stricmp(progname, p->name)==0 )
  91.                         break;
  92.                 }
  93.                 rc = p->rc;
  94.             }
  95.         }
  96.         if( Args.rc2 )
  97.             SetIoErr(*Args.rc2);
  98.         FreeArgs(rdargs);
  99.     }
  100.     else {
  101.         PrintFault(IoErr(), THIS_PROGRAM);
  102.         rc = RETURN_ERROR;
  103.     }
  104.     _exit((int)rc);
  105. }
  106.  
  107.